#include "msg_impl.h"
#include "../big_buf/head.h"
#include "../global/head.h"
#include "../buf/head.h"
#include "../xos/head.h"

/////////////////////////////////////////////////////////////////////////////////////////
// 
namespace xos
{
    msg_impl::msg_impl()
    {
        using namespace std;
        memset( m_lpRun, 0, sizeof( m_lpRun ) );
        memset( m_lpBigBuf, 0, sizeof( m_lpBigBuf ) );
        memset( m_lpBuf, 0, sizeof( m_lpBuf ) );
        memset( m_szStr, 0, sizeof( m_szStr ) );
        memset( m_lpData, 0, sizeof( m_lpData ) );
        memset( m_dlData, 0, sizeof( m_dlData ) );
        memset( m_nData, 0, sizeof( m_nData ) );
        memset( m_i64, 0, sizeof( m_i64 ) );
        memset( m_bData, 0, sizeof( m_bData ) );
        m_data_list.clear();
    }

    msg_impl::~msg_impl()
    {
        term();
    }

    ////////////////////////////////////////////////////////////////////////////////////
    // 

    int msg_impl::init()
    {
        int nRet = 0;
        return nRet;
    }

    int msg_impl::term_no_init()
    {
        int nRet = 0;
        m_data_list.clear();
        return nRet;
    }

    int msg_impl::term()
    {
        int nRet = 0;

        memset( m_lpRun, 0, sizeof( m_lpRun ) );
        memset( m_lpBigBuf, 0, sizeof( m_lpBigBuf ) );
        memset( m_lpBuf, 0, sizeof( m_lpBuf ) );
        memset( m_szStr, 0, sizeof( m_szStr ) );
        memset( m_lpData, 0, sizeof( m_lpData ) );
        memset( m_dlData, 0, sizeof( m_dlData ) );
        memset( m_nData, 0, sizeof( m_nData ) );
        memset( m_i64, 0, sizeof( m_i64 ) );
        memset( m_bData, 0, sizeof( m_bData ) );
        m_data_list.clear();

        return nRet;
    }

    ////////////////////////////////////////////////////////////////////////////////////
    // 

    // 
    // go though
    // 
    void * msg_impl::begin()
    {
        void * p = 0;
        m_begin = m_data_list.begin();

        if( m_data_list.end() != m_begin )
        {
            p = *m_begin;
            m_begin++;
        }

        return p;
    }

    void * msg_impl::next()
    {
        void * p = 0;

        if( m_data_list.end() != m_begin )
        {
            p = *m_begin;
            m_begin++;
        }

        return p;
    }

    // 
    // front list operation
    // 
    int msg_impl::push_front( void * pData )
    {
        int nRet = 0;
        m_data_list.push_front( pData );
        return nRet;
    }

    void * msg_impl::front()
    {
        void * pRet = 0;
        
        if( m_data_list.size() > 0 )
        {
            pRet = m_data_list.front();
        }

        return pRet;
    }

    void msg_impl::pop_front()
    {
        m_data_list.pop_front();
    }

    // 
    // back list operation
    // 
    int msg_impl::push_back( void * pData )
    {
        int nRet = 0;
        m_data_list.push_back( pData );
        return nRet;
    }

    void * msg_impl::back()
    {
        void * pRet = 0;
        
        if( m_data_list.size() > 0 )
        {
            pRet = m_data_list.back();
        }

        return pRet;
    }

    void msg_impl::pop_back()
    {
        m_data_list.pop_back();
    }

    // 
    // string
    // 
    const char * msg_impl::get_str( int nIndex, char * lpszStr, int nLen, int * lpnLenNeeded )
    {
        const char * lpRet = m_szStr[nIndex];

        if( lpnLenNeeded )
        {
            *lpnLenNeeded = ( int )strlen( lpRet );
        }

        if( lpszStr )
        {
            strcpy( lpszStr, lpRet );
        }

        return lpRet;
    }

    // 
    // i_big_buf
    // 
    i_big_buf * msg_impl::get_big_buf( int nIndex, i_big_buf ** lppBigBuf )
    {
        i_big_buf * lpBuf = m_lpBigBuf[nIndex];

        if( lppBigBuf )
        {
            *lppBigBuf = lpBuf;
        }

        return lpBuf;
    }

    int msg_impl::set_big_buf( int nIndex, i_big_buf * lpBigBuf )
    {
        int nRet = 0;
        m_lpBigBuf[nIndex] = ( big_buf_impl * )lpBigBuf;
        return nRet;
    }

    // 
    // i_buf
    // 
    i_buf * msg_impl::get_buf( int nIndex, i_buf ** lppBuf )
    {
        i_buf * lpBuf = m_lpBuf[nIndex];

        if( lppBuf )
        {
            *lppBuf = lpBuf;
        }

        return lpBuf;
    }

    int msg_impl::set_buf( int nIndex, i_buf * lpBuf )
    {
        int nRet = 0;
        m_lpBuf[nIndex] = ( buf_impl * )lpBuf;
        return nRet;
    }

    int msg_impl::set_str( int nIndex, const char * lpszStr )
    {
        int nRet = 0;

        char * lpRet = m_szStr[nIndex];
        strcpy( lpRet, lpszStr );

        return nRet;
    }

    // 
    // double
    // 
    double msg_impl::get_double( int nIndex, double * lpdlData )
    {
        if( lpdlData )
        {
            *lpdlData = m_dlData[nIndex];
        }

        return m_dlData[nIndex];
    }

    int msg_impl::set_double( int nIndex, double dlData )
    {
        int nRet = 0;
        m_dlData[nIndex] = dlData;
        return nRet;
    }

    // 
    // void *
    // 
    void * msg_impl::get_void( int nIndex, void ** lppData )
    {
        void * pRet = m_lpData[nIndex];

        if( lppData )
        {
            *lppData = pRet;
        }

        return pRet;
    }

    int msg_impl::set_void( int nIndex, void * lpData )
    {
        int nRet = 0;
        m_lpData[nIndex] = lpData;
        return nRet;
    }

    // 
    // int
    // 
    int msg_impl::get_int( int nIndex, int * lpnInt )
    {
        int nRet = m_nData[nIndex];

        if( lpnInt )
        {
            *lpnInt = nRet;
        }

        return nRet;
    }

    int msg_impl::set_int( int nIndex, int nInt )
    {
        int nRet = 0;
        m_nData[nIndex] = nInt;
        return nRet;
    }

    // 
    // xos_i64
    // 
    xos_i64 msg_impl::get_i64( int nIndex, xos_i64 * lpi64 )
    {
        if( lpi64 )
        {
            *lpi64 = m_i64[nIndex];
        }

        return m_i64[nIndex];
    }

    int msg_impl::set_i64( int nIndex, xos_i64 i64 )
    {
        m_i64[nIndex] = i64;
        return 0;
    }

    // 
    // bool
    // 
    bool msg_impl::get_bool( int nIndex, bool * lpBool )
    {
        if( lpBool )
        {
            *lpBool = m_bData[nIndex];
        }

        return m_bData[nIndex];
    }

    int msg_impl::set_bool( int nIndex, bool bBool )
    {
        m_bData[nIndex] = bBool;
        return 0;
    }

    // 
    // i_runnable
    // 
    i_runnable * msg_impl::get_runnable( int nIndex, i_runnable ** lppRun )
    {
        i_runnable * lpBuf = m_lpRun[nIndex];

        if( lppRun )
        {
            *lppRun = lpBuf;
        }

        return lpBuf;
    }

    int msg_impl::set_runnable( int nIndex, i_runnable * lpRun )
    {
        int nRet = 0;
        m_lpRun[nIndex] = lpRun;
        return nRet;
    }

    // 
    // release
    // 
    int msg_impl::release_no_init()
    {
        int nRet = 0;

        term_no_init();
        put_back_lock();

        return nRet;
    }

    // 
    // release
    // 
    int msg_impl::release()
    {
        int nRet = 0;

        term();
        put_back_lock();

        return nRet;
    }

} // xos
